package com.cs275.noteshare.Utilities;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;

import javax.net.ssl.HttpsURLConnection;

import com.cs275.noteshare.RequestType;

public class HTTP {

	private HTTP() {
	}

	private static final String USER_AGENT = "Mozilla/5.0";

	private static String formatParams(final Map<String, String> params) {
		StringBuilder sb = new StringBuilder();

		for (final Map.Entry<String, String> entry : params.entrySet()) {
			sb.append(entry.getKey());
			sb.append("=");
			sb.append(entry.getValue());
			sb.append("&");
		}

		sb.deleteCharAt(sb.length() - 1);
		String formatted;
		try {
			formatted = URLEncoder.encode(sb.toString(), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			formatted = "";
			e.printStackTrace();
		}
		return formatted;
	}

	public static String doRequest(final RequestType type, final String url,
			final Map<String, String> params) {
		return type == RequestType.POST ? doPost(url, formatParams(params))
				: doGet(url, formatParams(params));
	}

	private static String doGet(final String url, final String params) {
		StringBuilder urlBuilder = new StringBuilder();
		StringBuilder response = new StringBuilder();

		urlBuilder.append(url);
		if (!params.isEmpty()) {
			urlBuilder.append("?").append(params);
		}
		 
		try{
			URL obj = new URL(urlBuilder.toString());
			HttpURLConnection con = (HttpURLConnection) obj.openConnection();
	 
			// optional default is GET
			con.setRequestMethod("GET");
	 
			//add request header
			con.setRequestProperty("User-Agent", USER_AGENT);
	 
			//int responseCode = con.getResponseCode();
	 
			BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
			String inputLine;
	 
			while ((inputLine = in.readLine()) != null) {
				response.append(inputLine);
			}
			in.close();
		}catch(Exception e){
			
		}
 
		return response.toString();
		
	}

	private static String doPost(final String url, final String params) {
		
		StringBuilder response = new StringBuilder();

		try{
			URL obj = new URL(url);
			HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
	
			// add reuqest header
			con.setRequestMethod("POST");
			con.setRequestProperty("User-Agent", USER_AGENT);
			con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
	
			// Send post request
			con.setDoOutput(true);
			DataOutputStream wr = new DataOutputStream(con.getOutputStream());
			wr.writeBytes(params);
			wr.flush();
			wr.close();
	
			// int responseCode = con.getResponseCode();
	
			BufferedReader in = new BufferedReader(new InputStreamReader(
					con.getInputStream()));
			String inputLine;
	
			while ((inputLine = in.readLine()) != null) {
				response.append(inputLine);
			}
			in.close();
		}catch(Exception e){
			
		}

		return response.toString();

	}

}
